package fr.norsys.mapper.console.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;

import fr.norsys.mapper.console.model.ConsoleCst;
import fr.norsys.mapper.console.model.Identifiant;
import fr.norsys.mapper.console.model.IdentifiantComponent;
import fr.norsys.mapper.console.service.IdentifiantService;

public class IdentifiantAction extends BaseAppDispatchAction {
	private static Log log = LogFactory.getLog(IdentifiantAction.class);

	private IdentifiantService identifiantService;

	/**
	 * Action to list the identifiants components of the complete identifiant of
	 * the resource.
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("list method called");
		}
		DynaActionForm identifiantForm = (DynaActionForm) form;
		String currentId = (String) request.getParameter(ConsoleCst.CURRENT_ID);
		List identifiants = (List) request.getSession().getAttribute(
				ConsoleCst.IDS_SESSION_BEAN);
		if (currentId != null && currentId.length() > 0) {
			identifiants = identifiantService.list(currentId);
		} else {
			identifiants = new ArrayList();
		}

		request.getSession().setAttribute(ConsoleCst.IDS_SESSION_BEAN,
				identifiants);
		identifiantForm.set(ConsoleCst.ID_REQUEST_BEAN, new Identifiant(
				currentId));
		return mapping.findForward("index");
	}

	/**
	 * Action to get the complete identifiant of the resource.
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getCompleteId(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("list method called");
		}
		DynaActionForm identifiantForm = (DynaActionForm) form;
		List identifiants = (List) request.getSession().getAttribute(
				ConsoleCst.IDS_SESSION_BEAN);
		String currentId = identifiantService.getCompleteId(identifiants);
		request.getSession().setAttribute(ConsoleCst.IDS_SESSION_BEAN,
				identifiants);
		identifiantForm.set(ConsoleCst.ID_REQUEST_BEAN, new Identifiant(
				currentId));
		return mapping.findForward("index");
	}

	/**
	 * Action to delete an identifiant component from the list in session and
	 * from the complete identifiant
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("delete method called");
		}
		DynaActionForm identifiantForm = (DynaActionForm) form;
		String id = request.getParameter("id");
		identifiantService.delete(id, (List) request.getSession().getAttribute(
				ConsoleCst.IDS_SESSION_BEAN));
		String currentId = identifiantService.getCompleteId((List) request
				.getSession().getAttribute(ConsoleCst.IDS_SESSION_BEAN));
		identifiantForm.set(ConsoleCst.ID_REQUEST_BEAN, new Identifiant(
				currentId));
		return mapping.findForward("index");
	}

	/**
	 * Action to display an IdentifiantComponent of an Identifiant in session
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("edit method called");
		}
		String id = request.getParameter("id");
		DynaActionForm identifiantForm = (DynaActionForm) form;
		if (id != null && !"".equals(id)) {
			IdentifiantComponent identifiant = identifiantService.get(id,
					(List) request.getSession().getAttribute(
							ConsoleCst.IDS_SESSION_BEAN));
			identifiantForm.set(ConsoleCst.ID_COMP_REQUEST_BEAN, identifiant);
		}
		return mapping.findForward("popup");
	}

	public IdentifiantService getIdentifiantService() {
		return identifiantService;
	}

	public void setIdentifiantService(IdentifiantService identifiantService) {
		this.identifiantService = identifiantService;
	}
}